import React, {Component} from 'react';
import './index.css'
export default class Count extends Component {

    state = {
        count: 0
    }

    increment = () => {
        const {value} = this.selectValue;
        let {count} = this.state;
        this.setState({
            count: count + value * 1
        })
    }

    decrement = () => {
        const {value} = this.selectValue;
        let {count} = this.state;
        this.setState({
            count: count - value * 1
        })
    }

    incrementIfOdd = () => {
        const {value} = this.selectValue;
        let {count} = this.state;
        if (count % 2 !== 0) {
            this.setState({
                count: count + value * 1
            })
        }

    }

    incrementAsync = () => {
        setTimeout(() => {
            const {value} = this.selectValue;
            let {count} = this.state;
            this.setState({
                count: count + value * 1
            })
        },  1000)
    }

    render() {
        return (
            <div>
                <h1>当前和为：{this.state.count}</h1>
                <select className="sel" ref={c => this.selectValue = c}>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                </select>

                <div className="btns">
                    <button onClick={this.increment}>+</button>
                    <button onClick={this.decrement}>-</button>
                    <button onClick={this.incrementIfOdd}>当前为奇数时加</button>
                    <button onClick={this.incrementAsync}>异步+</button>
                </div>
            </div>
        );
    }
}